//
//  GyyImagCenter.m
//  个人项目
//
//  Created by 高永杨 on 16/4/30.
//  Copyright © 2016年 高永杨. All rights reserved.
//

#import "GyyImagCenter.h"
#import "UIView+GyyViewExtension.h"
@implementation GyyImagCenter

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//-(void)awakeFromNib
//{
//    self.titleLabel.textAlignment = NSTextAlignmentCenter;
//}
//-(instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self setcenterAlignment];
//    }
//    return self;
//}
//-(void)setcenterAlignment
//{
//self.titleLabel.textAlignment = NSTextAlignmentCenter;
//}
//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//    self.imageView.y = 0;
//    self.imageView.x = 0;
//    self.imageView.width = self.width;
//    self.imageView.height = self.width;
//    self.titleLabel.x = 0;
//    self.titleLabel.y = self.imageView.height+5;
//    self.titleLabel.width = self.width;
//    self.titleLabel.height = self.height - self.titleLabel.y;
//}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self centerImageAndTitle];
}

#pragma mark - center img and title
/// 图片和文字上下排列, 居中对齐
- (void)centerImageAndTitle {
    CGSize imageSize = self.imageView.frame.size;
    [self.titleLabel sizeToFit];
    CGSize titleSize = self.titleLabel.frame.size;
    NSString *text = self.titleLabel.text;
    if ([text respondsToSelector:@selector(sizeWithAttributes:)]) {
        titleSize = [text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    }
    CGFloat totalHeight = (imageSize.height+titleSize.height);
    
    self.imageEdgeInsets = UIEdgeInsetsMake(-(totalHeight-imageSize.height), 0, 0, -titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width,titleSize.height/2-70, 0);
    
}
-(void)awakeFromNib
{
    [super awakeFromNib];
    self.imageView.contentMode = self.titleLabel.contentMode = UIViewContentModeCenter;
}
@end
